package com.tfleo.mealssenger.balloon;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import com.google.android.maps.MapView;
import com.tfleo.mealssenger.PlaceActivity;
import com.tfleo.mealssenger.object.Place;
import com.tfleo.mealssenger.utility.IntentExtras;


public class PlacesOverlay extends BalloonItemizedOverlay<CustomOverlayItem> {
	private ArrayList<CustomOverlayItem> mOverlays = new ArrayList<CustomOverlayItem>();
	private Context mContext;
	
	public PlacesOverlay(Drawable defaultMarker, MapView mapView) {
		super(boundCenterBottom(defaultMarker), mapView);
		mContext = mapView.getContext();
	}

	@Override
	protected CustomOverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	@Override
	protected boolean onBalloonTap(int index, CustomOverlayItem item) {
		//TODO: intent details
		Intent i = new Intent(mContext, PlaceActivity.class); 
		i.putExtra(IntentExtras.PLACE_ID, mOverlays.get(index).getId());
	    i.putExtra(IntentExtras.PLACE_NAME, mOverlays.get(index).getTitle());
		
        mContext.startActivity(i); 
		return true;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
		/*Paint paintStroke = new Paint(); 
		 
        paintStroke.setStrokeWidth(8); 
        paintStroke.setStyle(Paint.Style.STROKE); 
        paintStroke.setColor(Color.rgb(92, 51, 23));
        Point screenPoint = new Point();
        for(int i = 0; i < mOverlays.size(); i++) {
        	mapView.getProjection().toPixels(mOverlays.get(i).getPoint(), screenPoint);
        	final Rect rect = new Rect(screenPoint.x, screenPoint.y, screenPoint.x+72, screenPoint.y+72); 
            canvas.drawRect(rect, paintStroke);
        }*/
		
		super.draw(canvas, mapView, false);

	}
	
	public void addOverlay(CustomOverlayItem overlay) {
	    mOverlays.add(overlay);
	    populate();
	    setLastFocusedIndex(-1);
	}
	
	public void clear() {
		mOverlays.clear();
		populate();
		setLastFocusedIndex(-1);
	}
}
